﻿define(['esri/layers/GraphicsLayer', 'esri/PopupTemplate', 'esri/Graphic'], function (
    GraphicsLayer,
    PopupTemplate,
    Graphic
) {
    var PolygonLayer = GraphicsLayer.createSubclass({
        setData: function (viewUtils, layer) {
            var me = this;
            if (!viewUtils) {
                return;
            }
            me.viewUtils = viewUtils;
            var options = layer.layer || {};
            var data = options.data || [];
            var popupOptions = options.popupOptions;
            var popupTemplateOptions = options.popupTemplateOptions;
            var symbolFieldName = options.symbolFieldName || 'symbol';
            var ringsFieldName = options.ringsFieldName || 'rings';
            var sr = options.sr;
            var symbol = {
                type: 'simple-fill',
                color: [51, 51, 204, 0.9],
                style: 'solid',
                outline: {
                    color: 'white',
                    width: 1,
                },
            };
            data.map((item, index) => {
                let polgon = { type: 'polygon', rings: [] };
                if (ringsFieldName) {
                    polgon.rings = item[ringsFieldName];
                }
                if (item[symbolFieldName]) {
                    let symbol = this.getSymbol(item[symbolFieldName]);
                    me.addGraphic(polgon, symbol, item, popupTemplateOptions, sr);
                    if (index == data.length - 1) {
                        if (typeof options.callback == 'function') {
                            options.callback(layer);
                        }
                    }
                } else {
                    me.addGraphic(polgon, symbol, item, popupTemplateOptions, sr);
                    if (index == data.length - 1) {
                        if (typeof options.callback == 'function') {
                            options.callback(layer);
                        }
                    }
                }
            });
        },
        addGraphic: function (geometry, symbol, item, popupTemplateOptions, sr) {
            var me = this;
            var popupTemplate = null;
            if (popupTemplateOptions) {
                popupTemplate = new PopupTemplate();
                popupTemplate.title = popupTemplateOptions.getTitle(item);
                popupTemplate.content = popupTemplateOptions.getContent(item);
            }

            var graphic = this.createGraphic({
                geometry: geometry,
                symbol: symbol,
                attributes: item,
                popupTemplate: popupTemplate,
            });

            me.add(graphic);
        },
        getGraphicLocation: function (viewUtils, esriEvent) {
            if (!esriEvent || !esriEvent.graphic) {
                return;
            }
            if (!viewUtils) {
                return;
            }

            var map = viewUtils.getMap();
            if (!map) {
                return;
            }

            var geometry = esriEvent.mapPoint || {};

            var lngFieldName = 'x';
            var latFieldName = 'y';
            var location = {};
            location[lngFieldName] = geometry.longitude;
            location[latFieldName] = geometry.latitude;
            return location;
        },
        createGraphic(t) {
            t = Object.assign(
                { geometry: null, symbol: null, attributes: null, popupTemplate: null },
                t
            );
            let g = new Graphic(t);
            return g;
        },
        getSymbol(t) {
            switch (t.type) {
                case 'picture-fill':
                    return t;
                case 'picture-marker':
                    return t;
                case 'simple-fill':
                    return t;
                case 'simple-line':
                    return t;
                case 'simple-marker':
                    return t;
                case 'text':
                    return t;
                default:
                    return t;
            }
        },
    });
    return PolygonLayer;
});
